/*
 * ClassicResultsPanel.java
 *
 * Created on 16 March 2007, 8:51
 */
package fasea.ui.search;

import fasea.constants.Constants;
import fasea.index.filehandler.FileHandlerException;
import fasea.search.SearchResults;
import fasea.search.documents.Document;
import fasea.ui.utils.ImageControl;
import java.awt.Color;
import java.io.File;
import javax.swing.Icon;

/**
 *
 * @author gvasil
 */
public class ClassicResultsPanel extends ResultsPanel {

    private int RESULTS_PER_FRAME = Constants.getMaxSearchResults();
    private int currentSelectedEntryIndex = 0;

    /**
     * Creates new form ClassicResultsPanel
     */
    public ClassicResultsPanel() {
        this.imageControl = ImageControl.getImageControl();

        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.GridLayout(this.RESULTS_PER_FRAME, 1, 0, 1));
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void markPanel(int index) {
        if (index >= 0 && index < getComponents().length ) {
            ClassicResultPanel resultPanel = (ClassicResultPanel) this.getComponent(currentSelectedEntryIndex);
            resultPanel.mark(false);
            currentSelectedEntryIndex = index;
            resultPanel = (ClassicResultPanel) this.getComponent(currentSelectedEntryIndex);
            resultPanel.mark(true);
        }
    }

    private void printHit(Document doc, int score, ClassicResultPanel resultPanel) {
        String title = doc.getTitle();
        String path = doc.getPath();
        long size = doc.getSize();
        resultPanel.setTitle(title);
        resultPanel.getTitleLabel().setToolTipText(path);
        resultPanel.setScoreIcon(this.imageControl.getStarsIcon(score));
        String folder = path.substring(0, path.lastIndexOf(File.separatorChar) + 1);
        resultPanel.setPath(folder);
        resultPanel.getPathLabel().setToolTipText(folder);

        try {
            Icon icon = doc.getThumbnail();
            resultPanel.getIconLabel().setText("");
            resultPanel.setIcon(icon);
        } catch (FileHandlerException e) {
            resultPanel.getIconLabel().setForeground(Color.RED);
            resultPanel.getIconLabel().setText("File not found");
            resultPanel.getIconLabel().setIcon(null);
        }
    }

    @Override
    public void printResults(SearchResults results) {
        if (rendererThread != null) {
            try {
                rendererThread.join();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        rendererThread = new RenderThread(results);
        this.removeAll();
        rendererThread.start();
    }

    @Override
    public void selectNextPanel() {
        int nextIndex = currentSelectedEntryIndex + 1;
        markPanel(nextIndex);
    }

    @Override
    public void selectPreviousPanel() {
        int previousIndex = currentSelectedEntryIndex - 1;
        markPanel(previousIndex);
    }
    
    public int getSelectedPanelIndex(){
        return currentSelectedEntryIndex;
    }
    
    private RenderThread rendererThread;
    private ImageControl imageControl;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private class RenderThread extends Thread {

        private SearchResults results;

        RenderThread(SearchResults results) {
            this.results = results;
        }

        @Override
        public void run() {
            for (int i = 0; i < results.size(); ++i) {
                ClassicResultPanel resultPanel = new fasea.ui.search.ClassicResultPanel(i, ClassicResultsPanel.this);
                add(resultPanel);
                printHit(results.getDocument(i),
                        (int) Math.ceil(results.getNormelizedScore(i) * 5),
                        resultPanel);

            }
            ClassicResultsPanel.this.updateUI();
            markPanel(0);
        }
    }
}
